package com.dona.imsidetector.db.dao;

import android.util.Log;

import com.dona.imsidetector.db.XUtilsManager;
import com.dona.imsidetector.db.entity.Frequency;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.db.table.DbModel;
import org.xutils.ex.DbException;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class FreqDao {

    private static final String TAG = "FreqDao";
    private static DbManager dbManager = XUtilsManager.getInstance().getDbManager();


    public static String add(Frequency frequency){

        try {
            dbManager.save(frequency);
        }catch (DbException e){
            e.printStackTrace();
            return e.getMessage();
        }
        return null;
    }

    public static String clear(){
        try {
            dbManager.delete(Frequency.class);
        }catch (DbException e){
            e.printStackTrace();
            return e.getMessage();
        }
        return null;
    }

    public static String delete(Frequency frequency){
        try {
            dbManager.delete(frequency);
        }catch (DbException e){
            e.printStackTrace();
            return e.getMessage();
        }
        return null;
    }

    public static Frequency find(String uuid){
        try {
            return dbManager.selector(Frequency.class).where("uuid","=", uuid).findFirst();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    public static List<Frequency> findAll(){
        try {
            return dbManager.findAll(Frequency.class);
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    public static List<Frequency> findByIspAndDeviceId(Integer isp, Integer deviceId){
        try {
            WhereBuilder b = WhereBuilder.b();
            b.and("isp","=",isp);
            b.and("deviceId","=", deviceId);
            return dbManager.selector(Frequency.class)
                    .where(b)
                    .orderBy("freq")
                    .findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    public static List<Frequency> findByIspDeviceId(Integer deviceId){
        try {
            return dbManager.selector(Frequency.class)
                    .where("deviceId","=",deviceId)
                    .orderBy("freq")
                    .findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    public static List<Frequency> findByIspAndBand(Integer isp,String band){
        try {
            WhereBuilder b = WhereBuilder.b();
            b.and("isp","=",isp);
            b.and("band","=", band);
            return dbManager.selector(Frequency.class)
                    .where(b)
                    .orderBy("freq")
                    .findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    public static List<Frequency> findByInBands(String[] bands){
        try {
            WhereBuilder b = WhereBuilder.b();
            b.and("band","in", bands);
            return dbManager.selector(Frequency.class)
                    .where(b)
                    .orderBy("freq")
                    .findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    public static List<Frequency> findByNotInBands(String[] bands){
        try {
            HashSet<String> bs = new HashSet<>();
            bs.addAll(Arrays.asList(bands));
            HashSet<String> not = new HashSet<>();
            List<DbModel> band = dbManager.selector(Frequency.class).groupBy("band").findAll();
            for(int i = 0; i < band.size(); i++){
                not.add(band.get(i).getString("band"));
            }
            Log.e(TAG, "findByNotInBands: not :" + not);
            not.removeAll(bs);


            WhereBuilder b = WhereBuilder.b();
            b.and("band","in", not.toArray());
            b.and("freq","<", 10000);
            return dbManager.selector(Frequency.class)
                    .where(b)
                    .orderBy("freq")
                    .findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    public static List<Frequency> findByIspAndBands(Integer isp,String[] bands){
        try {
            WhereBuilder b = WhereBuilder.b();
            if(isp != 4 ){
                b.and("isp","=", isp);
            }

            b.and("band","in", bands);
            return dbManager.selector(Frequency.class)
                    .where(b)
                    .orderBy("freq")
                    .findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }


    public static List<Frequency> findNoknwon(Integer device,Integer isp,String[] bands){
        try {
            WhereBuilder b = WhereBuilder.b();
            if(device == 1){

            }


            b.and("band","in", bands);
            return dbManager.selector(Frequency.class)
                    .where(b)
                    .orderBy("freq")
                    .findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }




}
